package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.alibaba.fastjson.JSON;
import com.mk.cloud.common.domain.PProduct;
import com.mk.cloud.common.enums.ErrorCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.http.H;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("product")
public class PProductController extends CookieService {

    @Inject
    private PProduct.Mapper pProductMapper;


    @Description("商品--商品列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PProduct> pProductList(
            @Description("筛选条件-姓名") String pdname,
            @Description("筛选条件-发布时间开始时间") String publishtimeb,
            @Description("筛选条件-发布时间结束时间") String publishtimee,
            @Description("筛选条件-品牌id") String brid,
            @Description("筛选条件-新店体验商品") String ppdid,
            @Description("筛选条件-商品状态") String pdstatus,
            @Description("筛选条件-商品类型") String pdtype,
            @Description("筛选条件-商品所属公司") String coid,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PProduct> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        if(S.isNotBlank(publishtimeb)){
            publishtimeb = publishtimeb+" 00:00:00";
        }
        if(S.isNotBlank(publishtimee)){
            publishtimee = publishtimee+" 23:59:59";
        }
        map.put("pdname", pdname);
        map.put("publishtimeb", publishtimeb);
        map.put("publishtimee", publishtimee);
        map.put("brid", brid);
        map.put("ppdid", ppdid);
        map.put("pdstatus", pdstatus);
        map.put("pdtype", pdtype);
        if(S.isNotBlank(me.getCoid())){
            map.put("coid", me.getCoid());
        }else {
            map.put("coid", coid);
        }
        query.setParas(map);
        //此处的sql是三个sql
        pProductMapper.getSQLManager().pageQuery("pProduct.select_product_page_list", PProduct.class, query);
        return query;
    }


    @Description("商品--商品详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PProduct getPProduct(@Description("商品id") String pdid) {
        if (S.isEmpty(pdid)) {
            return new PProduct();
        }
        return pProductMapper.single(pdid);
    }

    @Description("商品--商品保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePProduct(@Description("商品对象") PProduct product) {
        product.setPddescribe(S2.rightDoArticle(product.getPddescribe()));
        product.setCoid(me.getCoid());
        if (S.eq(product.getPdstatus(), GlobalValues.PD_STATUS_2.value())) {
            product.setPublishtime(DateUtils.getTimeNow());
            product.setPubuserid(me.getUserid());
        }
        if (S.isNotBlank(product.getPdid())) {
            pProductMapper.updateTemplateById(product);
            return product.getPdid();
        } else {
            String pdid = S2.UUID();
            product.setPdid(pdid);
            product.setCreatetime(DateUtils.getTimeNow());
            pProductMapper.insert(product);
            return pdid;
        }
    }

    @Description("商品--商品保存")
    @With({ICLogin.class, ITransacte.class})
    @PutAction("updatePdStatus.json")
    public String updatePdStatus(@Description("商品id") String pdid, @Description("商品状态")String pdStatus) {
        PProduct product = new PProduct();
        product.setPdid(pdid);
        product.setPdstatus(pdStatus);
        if (S.eq(pdStatus, GlobalValues.PD_STATUS_2.value())) {
            product.setPublishtime(DateUtils.getTimeNow());
            product.setPubuserid(me.getUserid());
        }
        pProductMapper.updateTemplateById(product);
        return pdid;
    }

    @Description("商品--商品删除")
    @With({ICLogin.class, ITransacte.class})
    @DeleteAction("{pdid}/del.json")
    public Integer delPProduct(@Description("商品id") String pdid) {
        PProduct product = pProductMapper.single(pdid);
        if (product!=null) {
            UserException.throwIf(S.eq(product.getPdstatus(), GlobalValues.PD_STATUS_2.value()), ErrorCode.E202001);
        }
        Integer count = pProductMapper.deleteById(pdid);
        return count;
    }


}
